
#include <boost/format.hpp>
#include "error.h"

error_exception::error_exception(utils::error_code ec, 
                                 const char* file,
                                 const char* function,
                                 uint32_t line) : ec_(ec) {
  Init(file, function, line);
}


error_exception::error_exception(utils::error_code ec,
                                 const char* extra_info,
                                 const char* file,
                                 const char* function,
                                 uint32_t line) 
    : ec_(ec), extra_info_(extra_info) {
  Init(file, function, line);
}

error_exception::error_exception(utils::error_code ec, 
                                  const std::string& extra_info,
                                  const char* file,
                                  const char* function,
                                  uint32_t line) 
    : ec_(ec), extra_info_(extra_info) {
  Init(file, function, line);
}

void error_exception::Init(const char* file, 
                           const char* function,
                           uint32_t line) {
  file_ = file;
  function_ = function;
  line_ = line;
}

const char* error_exception::what(void) const throw() {
  try {
    if (what_.empty()) {
      what_ = str(boost::format("[%s:%d:%s]%s:%s") % file_ % line_ % 
                  function_ % ec_.category().name() % ec_.message());
      if (!extra_info_.empty()) 
        what_ += "(" + extra_info_ + ")";
    }
    return what_.c_str();
  } catch(...) {
    return "exception throw inside error_exception::what";
  }
}

